package cheme.ProcessEditor.Figures;

import static cheme.ProcessEditor.Figures.IDecoratable.Alignment.BOTTOM;
import static cheme.ProcessEditor.Figures.IDecoratable.Alignment.LEFT;
import static cheme.ProcessEditor.Figures.IDecoratable.Alignment.RIGHT;
import static cheme.ProcessEditor.Figures.IDecoratable.Alignment.TOP;

import org.eclipse.draw2d.BorderLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LayoutManager;

import cheme.ProcessEditor.Figures.Util.CompartmentFig;

public class DecoratableFigure<T extends IFigure> extends ChemeFigure implements IDecoratable<T>
{
	IFigure leftCompartment;
	IFigure topCompartment;
	IFigure rightCompartment;
	IFigure bottomCompartment;
	T contentPane;
	
	public DecoratableFigure(T contentFigure)
	{
		setContentPane(contentFigure);
		setVisible(true);
	}
	
	@Override
	public BorderLayout getLayoutManager()
	{
		LayoutManager layout = super.getLayoutManager();
		
		if(null == layout)
		{
			layout = new BorderLayout();
			setLayoutManager(layout);
		}
		
		return (BorderLayout)layout;
	}
	
	public void add(IDecorator decorator)
	{
		switch(decorator.getDecorationAlignment())
		{
		case LEFT : getLeftCompartment().add(decorator); break;
		case TOP : getTopCompartment().add(decorator); break;
		case RIGHT : getRightCompartment().add(decorator); break;
		case BOTTOM : getBottomCompartment().add(decorator); break;
		default : throw new Error("Invalid add decoration alignment option.");
		}
	}
	
	public void remove(IDecorator decorator)
	{
		switch(decorator.getDecorationAlignment())
		{
		case LEFT : getLeftCompartment().remove(decorator); break;
		case TOP : getTopCompartment().remove(decorator); break;
		case RIGHT : getRightCompartment().remove(decorator); break;
		case BOTTOM : getBottomCompartment().remove(decorator); break;
		default : throw new Error("Invalid remove decoration alignment option.");
		}
	}
		
	public T getContentPane()
	{
		return contentPane;
	}

	public void setContentPane(T contentFigure)
	{
		if(null != getContentPane())
		{
			remove(getContentPane());
			getLayoutManager().remove(getContentPane());
		}
		
		this.contentPane = contentFigure;
		
		add(this.contentPane);
		getLayoutManager().setConstraint(this.contentPane, BorderLayout.CENTER);
	}

	public IFigure getBottomCompartment()
	{
		if(null == bottomCompartment)
		{
			bottomCompartment = new CompartmentFig(CompartmentFig.ALIGN_HORIZONTAL);
			add(bottomCompartment);
			getLayoutManager().setConstraint(bottomCompartment, BorderLayout.BOTTOM);
		}
		
		return bottomCompartment;
	}

	public void setBottomCompartment(IFigure bottomCompartment)
	{
		this.bottomCompartment = bottomCompartment;
	}

	public IFigure getLeftCompartment()
	{
		if(null == leftCompartment)
		{
			leftCompartment = new CompartmentFig(CompartmentFig.ALIGN_VERTICAL);
			
			add(leftCompartment);
			getLayoutManager().setConstraint(leftCompartment, BorderLayout.LEFT);
		}
		
		return leftCompartment;
	}

	public void setLeftCompartment(IFigure leftCompartment)
	{
		this.leftCompartment = leftCompartment;
	}

	public IFigure getRightCompartment()
	{
		if(null == rightCompartment)
		{
			rightCompartment = new CompartmentFig(CompartmentFig.ALIGN_VERTICAL);
			add(rightCompartment);
			getLayoutManager().setConstraint(rightCompartment, BorderLayout.RIGHT);
		}
		
		return rightCompartment;
	}

	public void setRightCompartment(IFigure rightCompartment)
	{
		this.rightCompartment = rightCompartment;
	}

	public IFigure getTopCompartment()
	{
		if(null == topCompartment)
		{
			topCompartment = new CompartmentFig(CompartmentFig.ALIGN_HORIZONTAL);
			add(topCompartment);
			getLayoutManager().setConstraint(topCompartment, BorderLayout.TOP);
		}
		
		return topCompartment;
	}

	public void setTopCompartment(IFigure topCompartment)
	{
		this.topCompartment = topCompartment;
	}
	
}
